package com.zwl.sjzmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwl.login.JwtUtils;
import com.zwl.model.LoginInfo;
import com.zwl.model.UserInfo;
import com.zwl.sjzmanagement.mapper.UserInfoMapper;
import com.zwl.sjzmanagement.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;



    @Autowired
    private StringRedisTemplate redisTemplate; // 一定要用StringRedisTemplate这个Bean对象

    @Override
    public LoginInfo Login(UserInfo userInfo) {
        //1. 调用mapper方法查询数据库
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getUsername, userInfo.getUsername()).eq(UserInfo::getPassword,userInfo.getPassword());
        UserInfo userInfo1 = userInfoMapper.selectOne(wrapper);

        //2. 判断
        if (userInfo1 != null) { //登录成功 - 生成JWT令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("id", userInfo1.getId());
            claims.put("username", userInfo1.getUsername());

            String jwt = JwtUtils.generateJwt(claims);

            System.out.println(jwt);
            return new LoginInfo(userInfo1.getId(), userInfo1.getUsername(), jwt);
        }

        return null;
    }
}
